package com.example.lin.controller;

import com.example.lin.common.R;
import com.example.lin.entity.pojo.Permission;
import com.example.lin.entity.pojo.User;
import com.example.lin.entity.vo.LoginUser;
import com.example.lin.entity.vo.RouterVo;
import com.example.lin.service.UserService;
import com.example.lin.utils.TreeUtil;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lzh
 * @since 2024-12-09
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 查询列表
     */
//    @PreAuthorize("hasAuthority('sys:user:list')")
    @GetMapping(value = "/list")
    public R list() {
        return R.ok(userService.list());
    }

//    @PreAuthorize("hasAuthority('sys:user:info')")
    @PostMapping(value = "/info")
    public R getInfo() {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return ObjectUtils.isEmpty(loginUser) ? R.ok(loginUser.getUser()) : R.error("用户不存在");
    }

    @GetMapping(value = "/person")
    public R getById(Long id) {
        User user = userService.getById(id);
        return ObjectUtils.isEmpty(user) ? R.ok(user) : R.error("用户不存在");
    }

//    @GetMapping("/router")
//    public R getMenu() {
//        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        List<Permission> permissions = loginUser.getUser().getPermissions();
//        List<Permission> collect = permissions.stream().filter(item -> !ObjectUtils.isEmpty(item) && item.getType() != 2).toList();
//        List<RouterVo> routers = TreeUtil.routerTree(collect, 0);
//        return R.ok(routers);
//    }
//
//    @GetMapping("/permission")
//    public R getPermission() {
//        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        List<Permission> permissions = loginUser.getUser().getPermissions();
//        List<Permission> collect = permissions.stream().filter(item -> !ObjectUtils.isEmpty(item)).toList();
//        List<Permission> permissionTree = TreeUtil.permissionTree(collect, 0);
//        return R.ok(permissionTree);
//    }

    /**
     * 新增单个数据
     */
    @PreAuthorize("hasAuthority('sys:user:save')") // 权限控制
    @PostMapping
    public R save(@RequestBody User user) {
        boolean flag = userService.save(user);
        return R.ok(flag).setMsg("保存成功");
    }

    /**
     * 新增多个数据
     */
    @PreAuthorize("hasAuthority('sys:user:save')") // 权限控制
    @PostMapping(value = "/batch")
    public R saveBatch(@RequestBody List<User> users) {
        boolean flag = userService.saveBatch(users);
        return R.ok(flag).setMsg("保存成功");
    }

    /**
     * 删除单个数据
     */
    @PreAuthorize("hasAuthority('sys:user:delete')") // 权限控制
    @DeleteMapping
    public R delete(Long id) {
        boolean flag = userService.removeById(id);
        return R.ok(flag).setMsg("删除成功");
    }

    /**
     * 删除多个数据
     */
    @PreAuthorize("hasAuthority('sys:user:delete')") // 权限控制
    @DeleteMapping(value = "/batch")
    public R deleteBatch(@RequestBody List<Long> ids) {
        boolean flag = userService.removeBatchByIds(ids);
        return R.ok(flag).setMsg("删除成功");
    }

    /**
     * 修改单个数据
     */
    @PreAuthorize("hasAuthority('sys:user:update')") // 权限控制
    @PutMapping
    public R update(@RequestBody User user) {
        boolean flag = userService.updateById(user);
        return R.ok(flag).setMsg("修改成功");
    }

    /**
     * 修改单个数据
     */
    @PreAuthorize("hasAuthority('sys:user:update')") // 权限控制
    @PutMapping(value = "/batch")
    public R updateBatch(@RequestBody List<User> users) {
        boolean flag = userService.updateBatchById(users);
        return R.ok(flag).setMsg("修改成功");
    }
}
